package com.eazy.lksy.web.controller;  
  
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eazy.lksy.web.model.Role;
import com.eazy.lksy.web.view.PageList;
import com.eazy.lksy.web.view.ResultVO;
import com.eazy.lksy.web.view.vo.RequestUser;
import com.eazy.lksy.web.view.vo.RequestUserRole;
import com.eazy.lksy.web.view.vo.ResponseUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lksy.web.common.BaseController;
import com.eazy.lksy.web.model.User;
import com.eazy.lksy.web.service.RoleService;
import com.eazy.lksy.web.service.UserService;
import com.eazy.lksy.web.utils.Bundle;
  

/**
 * 用户管理
 */
@Controller  
@RequestMapping("/user")  
public class UserController extends BaseController {  
  
    @Autowired  
    private UserService userService;  
    @Autowired
    private RoleService roleService;
    
    
    /**
     * 查询用户列表
     */
    @RequestMapping(value = "list" , method = RequestMethod.GET)  
    public ModelAndView list(Model model) {
        return new ModelAndView("user/user_list");  
    }

    /**
     * 异步获取数据
     */
    @RequestMapping(value = "data", method = RequestMethod.GET)
    @ResponseBody
    public PageList<ResponseUser> data(Integer start, Integer length, Integer draw) {
        PageList<ResponseUser> list = userService.getUserPage(start, length);
        list.setDraw(draw);
        return list;
    }


    /**
     * 添加用户角色
     */
    @RequestMapping(value = "addUserRole" , method = { RequestMethod.POST, RequestMethod.GET })  
    public String addUserRole(RequestUserRole request) {
        roleService.addUserRole(request);
    	return "redirect:/user/list";
    }  
    
    
    /**
     * 添加用户
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(RequestUser request) {
    	userService.addUserRole(request);
    	return ResultVO.success();
    }
    
    /**
     * 修改用户 
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String updateUser(HttpServletRequest request) {
    	Map<String, String> maps = getFormPage();
  //  	userService.update(new User(Bundle.convInt(maps.get("id")), maps.get("name"), maps.get("password")));
    	return "redirect:/user/list";
    }
  
    
    /**
     * 删除用户 
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequiresPermissions("sys:user:delete")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable String id) {
    	userService.deleteById(id);
    	int i = 10/0;
    	roleService.deleteUserRole(id);
    	return "redirect:/user/list";
    }
    
    /**
     * 跳转到设置角色页面
     */
    @RequiresPermissions("sys:user:toRole")
    @RequestMapping(value = "toRole/{id}", method = RequestMethod.GET)
    public ModelAndView toRole(@PathVariable String id) {
    	List<Map<String, Object>> self_role = roleService.getRoles(id); // 用户拥有角色
    	List<Role> all_role = roleService.select(); // 所有角色
    	
    	for (Role all : all_role) {
    	    all.setPoint(0);
			for (Map<String,Object> self : self_role) {
				if(Bundle.convStr(self.get("ID")).equals(Bundle.convStr(all.getId()))) {
					all.setPoint(1);
				}
			}
		}
    	
    	ModelMap map = new ModelMap("all",all_role);
    	map.put("id", id);
    	return new ModelAndView("user/user_role",map);
    }
    
    
    /**
     * 跳转到修改用户页面并获取用户ID
     */
    @RequestMapping(value = "toUpdate", method = RequestMethod.GET)
    public ModelAndView toUpdateUser(@RequestParam String id) {
    	ModelAndView modelAndView = new ModelAndView("/user/update");
    	modelAndView.addObject("user", getCurrentUser());
    	return modelAndView;
    }
    
    /**
     * 跳转到添加用户页面
     */
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "toAdd", method = RequestMethod.GET)
    public ModelAndView toAddUser(HttpServletRequest request) {
        List<Role> all_role = roleService.select(); // 所有角色
    	return new ModelAndView("user/user_add","role",all_role);
    }
}